package com.nf.teamwork.mall.controller.mall.foreground;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.nf.teamwork.mall.entity.CarouselPic;
import com.nf.teamwork.mall.entity.ProductCategory;
import com.nf.teamwork.mall.entity.UserInf;
import com.nf.teamwork.mall.service.*;
import com.nf.teamwork.mall.vo.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.Principal;
import java.util.List;


/**
 * @Author: Sherwin
 * @Classname: IndexController
 * @date: 2020/3/3 15:17
 * @Description: 首页界面
 */
@RestController
@Api(tags = "首页控制器IndexController")
public class IndexController {

    @Autowired
    private ProductCategoryService productCategoryService;

    @Autowired
    private CarouselPicService carouselPicService;

    @Autowired
    private UserInfService userInfService;

    @Autowired
    private CartInfService cartInfService;

    @Autowired
    private HotSellPicService hotSellPicService;

    @Autowired
    private ColumnInfService columnInfService;

    @Autowired
    private ProductInfService productInfService;

    @ApiOperation(value = "获取当前用户信息",notes = "返回的是userInf实体")
    @GetMapping("/user_inf/")
    public ResponseVO getUserInf(Principal principal){
        String account=principal.getName();
        return ResponseVO.newBuilder()
                .code(200)
                .data(userInfService.findByAccount(account))
                .message("ok")
                .build();
    }

    @ApiOperation(value = "获取当前登陆用户的购物车数量",notes = "返回的的是购物车商品数量")
    @GetMapping("/cart_infs/cart_product_count")
    public ResponseVO getCartProductCountByUserInfId(Principal principal){
        UserInf userInf=userInfService.findByAccount(principal.getName());
       int cartProductCount=cartInfService.getCartProductCountByUserInfId(userInf.getUserInfId());
       return ResponseVO.newBuilder()
               .code(200)
               .data(cartProductCount)
               .message("ok")
               .build();

    }

    @ApiOperation(value = "获取轮播图左侧栏目数据,也就是商品类型",notes = "返回的是ProdcutCategory集合")
    @GetMapping("/product_categories")
    public ResponseVO getProductCategoriesByParentId(){
        return ResponseVO.newBuilder()
                .code(200)
                .data(productCategoryService.getProductCategoriesByParentId(0))
                .message("ok")
                .build();
    }

    @ApiOperation(value="根据类型id得到其子级数据（商品类型集合）",notes = "传productCategoryId,返回的是ProuctCategory集合")
    @GetMapping("/product_categories/{productCategoryId}")
    public ResponseVO getProductCategoriesByProductCategoryId(@ApiParam(name = "productCategoryId",value = "商品类型id",required = true) @PathVariable int productCategoryId){
        return ResponseVO.newBuilder()
                .code(200)
                .data(productCategoryService.getProductCategoriesByProductCategoryId(productCategoryId))
                .message("ok")
                .build();


    }

    @ApiOperation(value="获取轮播图",notes ="返回的是CarouselPic集合" )
    @GetMapping("/carousel_pics")
    public ResponseVO getCarouselPics(){
        return ResponseVO.newBuilder()
                .code(200)
                .data(carouselPicService.findAll())
                .message("ok")
                .build();
    }

    @ApiOperation(value="获取商品热销图",notes = "返回的是HotSellPic集合")
    @GetMapping("/hot_sell_pics")
    public ResponseVO getHotSellPics(){
        return ResponseVO.newBuilder()
                .code(200)
                .data(hotSellPicService.findAll())
                .message("ok")
                .build();
    }

    @ApiOperation(value="获取栏目信息数据",notes = "返回ColumnInf集合")
    @GetMapping("/column_infs")
    public ResponseVO getColumnInfs(){
        return ResponseVO.newBuilder()
                .code(200)
                .data(columnInfService.findAll())
                .message("ok")
                .build();
    }

    @ApiOperation(value = "根据栏目id获取商品数据",notes = "传columnInfId,返回ProductInf集合")
    @GetMapping("/column_inf/{columnInfId}/product_infs")
    public ResponseVO getProductInfByColumnInfId(@ApiParam(name = "columnInfId",value = "栏目id",required = true) @PathVariable int columnInfId){
        return ResponseVO.newBuilder()
                .code(200)
                .data(productInfService.getProductInfByColumnInfId(columnInfId))
                .message("ok")
                .build();
    }



}
